package com.example.shop.commodity.controller;

import com.example.shop.base.BaseController;
import com.example.shop.base.Result;
import com.example.shop.common.shiro.ShiroUtils;
import com.example.shop.common.utils.LocalFileUtils;
import com.example.shop.domain.sys.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import com.example.shop.commodity.entity.ShopCategorys;
import com.example.shop.commodity.service.ShopCategorysService;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author hcj
 * @date 2021-09-21 19:06:34
 */
@Controller
@RequestMapping(value = "/shop/category")
public class ShopCategorysController extends BaseController {

    @Autowired
    private ShopCategorysService shopCategorysService;

    @Value("${system.profile.path}")
    private String profile;

    @Value("${system.profile.real_path}")
    private String realProfile;

    @ModelAttribute
    public ShopCategorys get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            ShopCategorys shopCategorys = new ShopCategorys();
            shopCategorys.setId(id);
            return shopCategorysService.get(shopCategorys);
        } else {
            return new ShopCategorys();
        }
    }

    @RequestMapping(value = "/get")
    @ResponseBody
    public ShopCategorys getById(ShopCategorys shopCategorys) {
        return shopCategorys;
    }

    /**
     * 新增修改页面
     *
     * @param shopCategorys
     * @param model
     * @return
     */
    @GetMapping("/form")
    public String form(ShopCategorys shopCategorys, Model model) {
        if (shopCategorys != null) {
            model.addAttribute("shopCategorys", shopCategorys);
        }
        return "shop/category/category_form";
    }

    @RequestMapping(value = "/list")
    public String list(ShopCategorys shopCategorys) {
        return "shop/category/category_list";
    }

    @RequestMapping(value = "/info")
    @ResponseBody
    public List<ShopCategorys> info(ShopCategorys shopCategorys) {
        return shopCategorysService.findList(shopCategorys);
    }


    @RequestMapping(value = "/findPage")
    @ResponseBody
    public PageInfo<ShopCategorys> findPage(ShopCategorys shopCategorys) {
        PageInfo<ShopCategorys> pageInfo = shopCategorysService.findPage(shopCategorys);
        return pageInfo;
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(ShopCategorys shopCategorys, MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            SysUser sysUser = ShiroUtils.getUser();
            String path = LocalFileUtils.upload(sysUser.getId(), profile, realProfile, file);
            shopCategorys.setIcon(path);
        }
        shopCategorysService.save(shopCategorys);
        return Result.success("保存成功");
    }

    @RequestMapping(value = "/logicalDeletion", method = RequestMethod.POST)
    @ResponseBody
    public Result logicalDeletion(@RequestParam("id") String id) {
        shopCategorysService.logicalDeletion(id);
        return Result.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestParam("ids") String[] ids) {
        shopCategorysService.delete(ids);
        return Result.success();
    }

}